﻿using CatUtils.Utils.Currency;
using MySql.Data.MySqlClient;
using System;
using System.Data;

namespace CatUtils.Utils.SpecialUtils.DataTool.Mysql
{
    /// <summary>
    /// Mysql帮助类
    /// </summary>
    public class MysqlTool
    {
        /// <summary>
        /// 创建数据库，返回-1代表失败
        /// </summary>
        /// <param name="DatabaseName"></param>
        /// <param name="CreateQuery"></param>
        /// <returns></returns>
        public int CreateTable(string DatabaseName, string CreateQuery)
        {
            return CreateTables(new MysqlConfig().ConnectionInit(DatabaseName), CreateQuery);
        }

        /// <summary>
        /// 判断数据是否存在
        /// </summary>
        /// <param name="DatabaseName"></param>
        /// <param name="SelectQuery"></param>
        /// <returns></returns>
        public bool DataExits(string DatabaseName, string SelectQuery)
        {
            return DataExits(new MysqlConfig().ConnectionInit(DatabaseName), SelectQuery);
        }

        /// <summary>
        /// 判断数据是否存在 存在返回true
        /// </summary>
        /// <param name="mySqlConnection"></param>
        /// <param name="SelectQuery"></param>
        /// <returns></returns>
        public bool DataExits(MySqlConnection mySqlConnection, string SelectQuery)
        {
            try
            {
                using (mySqlConnection)
                {
                    mySqlConnection.Open();
                    using (MySqlCommand cmd = new MySqlCommand(SelectQuery, mySqlConnection))
                    {
                        MySqlDataReader reader = cmd.ExecuteReader();
                        return reader.HasRows;//如果有数据则为true
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("查询失败了！");
                PrintLog.Log(ex);
            }
            return false;
        }

        /// <summary>
        /// mysql选择数据，若为空返回NULL
        /// </summary>
        /// <param name="SelectQuery"></param>
        /// <returns></returns>
        public DataTable Select(string SelectQuery)
        {
            return Select(new MysqlConfig().ConnectionInit(), SelectQuery);
        }

        /**
         *      SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
         *      在执行该命令时，如果关闭关联的 DataReader 对象，则关联的 Connection 对象也将关闭。
         */

        /// <summary>
        /// mysql选择数据，若为空返回NULL
        /// </summary>
        /// <param name="mySqlConnection"></param>
        /// <param name="SelectQuery"></param>
        /// <returns></returns>
        public DataTable Select(MySqlConnection mySqlConnection, string SelectQuery)
        {
            DataTable dataTable = new DataTable();
            try
            {
                using (mySqlConnection)
                {
                    mySqlConnection.Open();
                    Console.WriteLine(SelectQuery);
                    MySqlCommand cmd = new MySqlCommand(SelectQuery, mySqlConnection);
                    using (MySqlDataReader dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        if (dataReader.HasRows)
                        {
                            dataTable.Load(dataReader);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return dataTable;
        }

        /// <summary>
        ///  插入数据 返回-1代表失败
        /// </summary>
        /// <param name="DatabaseName"></param>
        /// <param name="InsertQuery"></param>
        /// <returns></returns>
        public int Insert(string DatabaseName, string InsertQuery)
        {
            return Insert(new MysqlConfig().ConnectionInit(DatabaseName), InsertQuery);
        }

        /// <summary>
        ///  插入数据 返回-1代表失败
        /// </summary>
        /// <param name="Localonnection"></param>
        /// <param name="CreateQuery"></param>
        /// <returns></returns>
        public int Insert(MySqlConnection Localonnection, string CreateQuery)
        {
            int Status = -1;
            try
            {
                Localonnection.Open();
                //CREATE TABLE `localtest`.`catdata5` ( `dataindex` INT NOT NULL , INDEX (`dataindex`)) ENGINE = MyISAM
                MySqlCommand cmd = new MySqlCommand(CreateQuery, Localonnection);

                Status = cmd.ExecuteNonQuery();
                Localonnection.Close();
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return Status;
        }

        /// <summary>
        /// 创建数据库，返回-1代表失败
        /// </summary>
        /// <param name="Localonnection"></param>
        /// <param name="CreateQuery"></param>
        /// <returns></returns>
        public int CreateTables(MySqlConnection Localonnection, string CreateQuery)

        {
            return Insert(Localonnection, CreateQuery);
        }

        /// <summary>
        /// 创建数据库
        /// </summary>
        /// <param name="DatabasesName"></param>
        /// <returns></returns>
        public int CreateDatabases(string DatabasesName)
        {
            return CreateDatabases(new MysqlConfig().ConnectionInit(), DatabasesName);
        }

        /// <summary>
        /// 创建数据库
        /// </summary>
        /// <param name="Localonnection"></param>
        /// <param name="DatabasesName"></param>
        /// <returns></returns>
        public int CreateDatabases(MySqlConnection Localonnection, string DatabasesName)
        {
            int Status = -1;
            try
            {
                Localonnection.Open();

                MySqlCommand cmd = new MySqlCommand(string.Format("create database {0};", DatabasesName), Localonnection);

                Status = cmd.ExecuteNonQuery();
                Localonnection.Close();
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return Status;
        }
    }
}